package org.apache.james.mime4j.field.structured;

import [I;
import java.io.InputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.Vector;

public class StructuredFieldParser
  implements StructuredFieldParserConstants
{
  private static int[] jj_la1_0;
  private Vector jj_expentries;
  private int[] jj_expentry;
  private int jj_gen;
  SimpleCharStream jj_input_stream;
  private int jj_kind;
  private final int[] jj_la1;
  public Token jj_nt;
  private int jj_ntk;
  private boolean preserveFolding;
  public Token token;
  public StructuredFieldParserTokenManager token_source;

  static
  {
    jj_la1_0();
  }

  public StructuredFieldParser(InputStream paramInputStream)
  {
    this(paramInputStream, null);
  }

  public StructuredFieldParser(InputStream paramInputStream, String paramString)
  {
    this.preserveFolding = false;
    this.jj_la1 = new int[2];
    this.jj_expentries = new Vector();
    this.jj_kind = -1;
    try
    {
      this.jj_input_stream = new SimpleCharStream(paramInputStream, paramString, 1, 1);
      this.token_source = new StructuredFieldParserTokenManager(this.jj_input_stream);
      this.token = new Token();
      this.jj_ntk = -1;
      this.jj_gen = 0;
      int i = 0;
      if (i < 2)
      {
        this.jj_la1[i] = -1;
        ++i;
      }
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      throw new RuntimeException(localUnsupportedEncodingException);
    }
  }

  public StructuredFieldParser(Reader paramReader)
  {
    this.preserveFolding = false;
    this.jj_la1 = new int[2];
    this.jj_expentries = new Vector();
    this.jj_kind = -1;
    this.jj_input_stream = new SimpleCharStream(paramReader, 1, 1);
    this.token_source = new StructuredFieldParserTokenManager(this.jj_input_stream);
    this.token = new Token();
    this.jj_ntk = -1;
    this.jj_gen = 0;
    for (int i = 0; i < 2; ++i)
      this.jj_la1[i] = -1;
  }

  public StructuredFieldParser(StructuredFieldParserTokenManager paramStructuredFieldParserTokenManager)
  {
    this.preserveFolding = false;
    this.jj_la1 = new int[2];
    this.jj_expentries = new Vector();
    this.jj_kind = -1;
    this.token_source = paramStructuredFieldParserTokenManager;
    this.token = new Token();
    this.jj_ntk = -1;
    this.jj_gen = 0;
    for (int i = 0; i < 2; ++i)
      this.jj_la1[i] = -1;
  }

  private final String doParse()
    throws ParseException
  {
    StringBuffer localStringBuffer = new StringBuffer(50);
    int i = 0;
    int j = 1;
    while (true)
    {
      while (true)
      {
        do
        {
          while (true)
          {
            int k;
            int l;
            if (this.jj_ntk == -1)
              k = jj_ntk();
            while (true)
              switch (k)
              {
              default:
                this.jj_la1[0] = this.jj_gen;
                return localStringBuffer.toString();
                k = this.jj_ntk;
              case 11:
              case 12:
              case 13:
              case 14:
              case 15:
              }
            if (this.jj_ntk == -1)
              l = jj_ntk();
            while (true)
              switch (l)
              {
              default:
                this.jj_la1[1] = this.jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
                l = this.jj_ntk;
              case 15:
              case 11:
              case 13:
              case 12:
              case 14:
              }
            Token localToken2 = jj_consume_token(15);
            if (j != 0)
              j = 0;
            while (true)
            {
              do
                while (true)
                  localStringBuffer.append(localToken2.image);
              while (i == 0);
              localStringBuffer.append(" ");
              i = 0;
            }
            localStringBuffer.append(jj_consume_token(11).image);
          }
          Token localToken1 = jj_consume_token(13);
          if (j != 0)
            j = 0;
          while (true)
          {
            do
              while (true)
                localStringBuffer.append(localToken1.image);
            while (i == 0);
            localStringBuffer.append(" ");
            i = 0;
          }
          jj_consume_token(12);
        }
        while (!(this.preserveFolding));
        localStringBuffer.append("\r\n");
      }
      jj_consume_token(14);
      i = 1;
    }
  }

  private final Token jj_consume_token(int paramInt)
    throws ParseException
  {
    Token localToken1 = this.token;
    if (localToken1.next != null)
      this.token = this.token.next;
    while (true)
    {
      this.jj_ntk = -1;
      if (this.token.kind != paramInt)
        break;
      this.jj_gen = (1 + this.jj_gen);
      return this.token;
      Token localToken2 = this.token;
      Token localToken3 = this.token_source.getNextToken();
      localToken2.next = localToken3;
      this.token = localToken3;
    }
    this.token = localToken1;
    this.jj_kind = paramInt;
    throw generateParseException();
  }

  private static void jj_la1_0()
  {
    int[] arrayOfInt = new int[2];
    arrayOfInt[0] = 63488;
    arrayOfInt[1] = 63488;
    jj_la1_0 = arrayOfInt;
  }

  private final int jj_ntk()
  {
    int i;
    Token localToken1 = this.token.next;
    this.jj_nt = localToken1;
    if (localToken1 == null)
    {
      Token localToken2 = this.token;
      Token localToken3 = this.token_source.getNextToken();
      localToken2.next = localToken3;
      i = localToken3.kind;
      this.jj_ntk = i;
    }
    while (true)
    {
      return i;
      i = this.jj_nt.kind;
      this.jj_ntk = i;
    }
  }

  public void ReInit(InputStream paramInputStream)
  {
    ReInit(paramInputStream, null);
  }

  public void ReInit(InputStream paramInputStream, String paramString)
  {
    try
    {
      this.jj_input_stream.ReInit(paramInputStream, paramString, 1, 1);
      this.token_source.ReInit(this.jj_input_stream);
      this.token = new Token();
      this.jj_ntk = -1;
      this.jj_gen = 0;
      int i = 0;
      if (i < 2)
      {
        this.jj_la1[i] = -1;
        ++i;
      }
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException)
    {
      throw new RuntimeException(localUnsupportedEncodingException);
    }
  }

  public void ReInit(Reader paramReader)
  {
    this.jj_input_stream.ReInit(paramReader, 1, 1);
    this.token_source.ReInit(this.jj_input_stream);
    this.token = new Token();
    this.jj_ntk = -1;
    this.jj_gen = 0;
    for (int i = 0; i < 2; ++i)
      this.jj_la1[i] = -1;
  }

  public void ReInit(StructuredFieldParserTokenManager paramStructuredFieldParserTokenManager)
  {
    this.token_source = paramStructuredFieldParserTokenManager;
    this.token = new Token();
    this.jj_ntk = -1;
    this.jj_gen = 0;
    for (int i = 0; i < 2; ++i)
      this.jj_la1[i] = -1;
  }

  public final void disable_tracing()
  {
  }

  public final void enable_tracing()
  {
  }

  public ParseException generateParseException()
  {
    this.jj_expentries.removeAllElements();
    boolean[] arrayOfBoolean = new boolean[18];
    for (int i = 0; i < 18; ++i)
      arrayOfBoolean[i] = false;
    if (this.jj_kind >= 0)
    {
      arrayOfBoolean[this.jj_kind] = true;
      this.jj_kind = -1;
    }
    for (int j = 0; j < 2; ++j)
      if (this.jj_la1[j] == this.jj_gen)
        for (int i1 = 0; i1 < 32; ++i1)
          if ((jj_la1_0[j] & 1 << i1) != 0)
            arrayOfBoolean[i1] = true;
    for (int k = 0; k < 18; ++k)
      if (arrayOfBoolean[k] != 0)
      {
        this.jj_expentry = new int[1];
        this.jj_expentry[0] = k;
        this.jj_expentries.addElement(this.jj_expentry);
      }
    [I[] arrayOf[I = new int[this.jj_expentries.size()][];
    for (int l = 0; l < this.jj_expentries.size(); ++l)
      arrayOf[I[l] = ((int[])(int[])this.jj_expentries.elementAt(l));
    return new ParseException(this.token, arrayOf[I, tokenImage);
  }

  public final Token getNextToken()
  {
    if (this.token.next != null)
      this.token = this.token.next;
    while (true)
    {
      this.jj_ntk = -1;
      this.jj_gen = (1 + this.jj_gen);
      return this.token;
      Token localToken1 = this.token;
      Token localToken2 = this.token_source.getNextToken();
      localToken1.next = localToken2;
      this.token = localToken2;
    }
  }

  public final Token getToken(int paramInt)
  {
    Token localToken1 = this.token;
    int i = 0;
    Object localObject = localToken1;
    if (i < paramInt)
    {
      Token localToken2;
      if (((Token)localObject).next != null)
        localToken2 = ((Token)localObject).next;
      while (true)
      {
        while (true)
        {
          ++i;
          localObject = localToken2;
        }
        localToken2 = this.token_source.getNextToken();
        ((Token)localObject).next = localToken2;
      }
    }
    return ((Token)localObject);
  }

  public boolean isFoldingPreserved()
  {
    return this.preserveFolding;
  }

  public String parse()
    throws ParseException
  {
    String str;
    try
    {
      str = doParse();
      return str;
    }
    catch (TokenMgrError localTokenMgrError)
    {
      throw new ParseException(localTokenMgrError);
    }
  }

  public void setFoldingPreserved(boolean paramBoolean)
  {
    this.preserveFolding = paramBoolean;
  }
}